﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.IO;
using System.Data.OleDb;

namespace VSM.Utils
{
    public class AppConstants
    {
        #region Session Objects
        //public const string CompanyName = "CompanyName";
        //public const string CompanyCode = "CompanyCode";
        //public const string UserCode = "UserCode";
        //public const string Password = "Password";
        //public const string UserID = "UserID";
        //public const string ConString = "ConString";
        //public const string IsSupperUser = "IsSupperUser";
        //public const string OutletCode = "OutletCode";
        //public const string OutletName = "OutletName";
        //public const string DATE = "dd/MM/yyyy";
        //public const string NUMBER_FORMAT = "#,##0.00";
        #endregion

        #region Properties

        public const string ID_User = "ID_User";
        public const string UserName = "UserName";
        public const string Password = "Password";
        public const string isAdmin = "isAdmin";
        public const string ID_Dealer = "ID_Dealer";
        public const string Default_Vendor = "Default_Vendor";
        public const string Email = "Email";
        public const string ID_Language = "ID_Language";
        public const string U_Department = "U_Department";
        public const string WhsCode = "WhsCode";

        public const string DATE = "dd/MM/yyyy";
        public const string NUMBER_FORMAT = "#,##0.000";
        public const string VSM = "VSM";
        public const string DEALER = "DEALER";

        public const string ICT = "ICT";
        public const string PARTS = "PAR";
        public const string SALES = "SAL";
        public const string SERVICES = "SVC";

        public const string EXTENSION_XLSX = "xlsx";

        #endregion

        public static bool isInteger(string integer)
        {
            try
            {
                int.Parse(integer);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public static bool isDateTime(string dateTime)
        {
            try
            {
                DateTime.ParseExact(dateTime, Utils.AppConstants.DATE, null, System.Globalization.DateTimeStyles.None);
            }
            catch
            {
                return false;
            }
            return true;
        }
        public static bool isDouble(string strDouble)
        {
            try
            {
                double.Parse(strDouble);
            }
            catch
            {
                return false;
            }
            return true;
        }
        /// <summary>
        ///
        /// </summary>
        /// <param name="strDate"></param>
        /// <returns></returns>
        public static DateTime ConvertToDate(string strDate)
        {
            return DateTime.ParseExact(strDate, Utils.AppConstants.DATE, null, System.Globalization.DateTimeStyles.None);
        }

        public enum SOStatus
        {
            New,
            Failed,
            Done
        }
        public static int GetColumnIndexByName(System.Web.UI.WebControls.GridView grid, string name)
        {
            for (int i = 0; i < grid.Columns.Count; i++)
            {
                if (grid.Columns[i].HeaderText.ToLower().Trim() == name.ToLower().Trim())
                {
                    return i;
                }
            }

            return -1;
        }
        public static DataTable ImportExcelFile(string fileName, string fileLocation)
        {
            DataTable dtExcelRecords = new DataTable();
            string fileExtension = Path.GetExtension(fileName);
            fileLocation = fileLocation + fileName;
            string connectionString = string.Empty;
            //Check whether file extension is xls or xslx
            if (fileExtension == ".xls")
            {
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (fileExtension == ".xlsx")
            {
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
            //Create OleDB Connection and OleDb Command
            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);

            con.Open();
            DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
            cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
            dAdapter.SelectCommand = cmd;
            dAdapter.Fill(dtExcelRecords);
            con.Close();
            return dtExcelRecords;
        }
        public static DataTable ImportCSVFile(string fileName, string fileLocation)
        {
            string[] Lines = File.ReadAllLines(fileLocation + fileName);
            string[] Fields;
            Fields = Lines[0].Split(new char[] { ',' });
            int Cols = Fields.GetLength(0);
            DataTable dt = new DataTable();
            for (int i = 0; i < Cols; i++)
                dt.Columns.Add(Fields[i].ToLower(), typeof(string));
            DataRow Row;
            for (int i = 1; i < Lines.GetLength(0); i++)
            {
                Fields = Lines[i].Split(new char[] { ',' });
                Row = dt.NewRow();
                for (int f = 0; f < Cols; f++)
                    Row[f] = Fields[f];
                dt.Rows.Add(Row);
            }
            return dt;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Str"></param>
        /// <param name="fullPath"></param>
        public static void WriteFile(string Str, string fullPath)
        {
            try
            {
                System.IO.StreamWriter oWrite = null;
                oWrite = System.IO.File.CreateText(fullPath);
                oWrite.WriteLine(Str);
                oWrite.Close();
            }
            catch (Exception ex)
            {
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="fullPath"></param>
        /// <returns></returns>
        public static string ReadFile(string fullPath)
        {
            try
            {
                return System.IO.File.ReadAllText(fullPath);
            }
            catch (Exception ex)
            {
            }
            return string.Empty;
        }
    }
}

public sealed class EmailTemplate
{
    public static readonly string path = "..\\..\\MailContent\\";

    public static readonly string pathSupportDocs = "..\\..\\SupportDocs\\Contract\\";

    public static readonly string POSubmitted = "POSubmit";
    public static readonly string POApproved = "POApproved";
    public static readonly string PORejected = "POReject";
    public static readonly string SOSubmitted = "SOSubmit";

    public static readonly string SOUpdated = "SOUpdate";
    public static readonly string VSMSOConfirmed = "VSMSOConfirm";
    public static readonly string VSMSORejected = "VSMSOReject";
    public static readonly string DEALERSOConfirmed = "DEALERSOConfirm";
    public static readonly string DEALERSORejected = "DEALERSOReject";
}

public sealed class SupportDocs
{
    public static readonly string pathSupportDocsContTemplate = "..\\..\\SupportDocs\\ContractTemplates\\";
    public static readonly string pathSupportDocsInvoiceTemplate = "..\\..\\SupportDocs\\InvoiceTemplates\\";
    public static readonly string fileImportParts = "~/SupportDocs/ImportTemplate/Parts.csv";
    public static readonly string pathPartsTemplate = "~/SupportDocs/PartsTemplates/";
}